package datastructure.book.dp._2_linear_pre_section.single.all;

import org.junit.jupiter.api.Test;

public class _5_Solution {

    public int maxProduct(int[] nums) {
        Integer dpPreL = null;
        Integer dpPreR = null;
        if (nums[0] < 0) {
            dpPreL = nums[0];
        } else if (nums[0] != 0){
            dpPreR = nums[0];
        }
        int max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == 0) {
                dpPreL = null;
                dpPreR = null;
                max = Math.max(max, 0);
                continue;
            }
            if (nums[i] > 0) {
                if (dpPreL != null) {
                    dpPreL *= nums[i];
                }
                if (dpPreR != null) {
                    dpPreR *= nums[i];
                } else {
                    dpPreR = nums[i];
                }
                max = Math.max(max, dpPreR);
            } else {
                if (dpPreL != null) {
                    int tempDpPreR = dpPreL*nums[i];
                    if (dpPreR != null) {
                        dpPreL = dpPreR * nums[i];
                    } else {
                        dpPreL = nums[i];
                    }
                    dpPreR = tempDpPreR;
                    max = Math.max(max, dpPreR);
                }else {
                    if (dpPreR != null) {
                        dpPreL = dpPreR * nums[i];
                    } else {
                        dpPreL = nums[i];
                    }
                    dpPreR = null;
                    max = Math.max(max, nums[i]);
                }
            }
        }
        return max;
    }

    @Test
    public void test(){
        int[] ints = {0,2};
        System.out.println(maxProduct(ints));
    }
}
